#include <stdio.h>
#include <string.h>

typedef unsigned char byte;

int main() {
  FILE* fp = fopen("font.txt", "r");
  if (fp == NULL) {
	  perror("Cannot open font.txt");
	  return 1;
  }
  FILE* fp2 = fopen("../include/character.h", "w");
  if (fp2 == NULL) {
	  fclose(fp);
	  perror("Cannot open ../include/character.h");
	  return 1;
  }
  byte ptr = 0, status = 0;
  byte data[8];
  memset(data, 0, 8);
  int ch;
  fprintf(fp2, "#ifndef CHARACTER_H\n#define CHARACTER_H\n\n");
  fprintf(fp2, "const byte characters[][8] = {\n");
  while ((ch = fgetc(fp)) != EOF) {
	  if (ch == '.') {
		  data[status] &= ~(1 << ptr);
	  } else if (ch == '*') {
		  data[status] |= 1 << ptr;
	  } else if (ch == '\n') {
		  status = -1;
		  ptr++;
	  } else if (ch == '$') {
		  ptr = 0;
		  status = -1;
		  fprintf(fp2, "\t{");
		  for (byte i = 0; i < 7; i++) {
			  fprintf(fp2, "0x%x, ", data[i]);
		  }
		  fprintf(fp2, "0x%x},\n", data[7]);
		  memset(data, 0, 8);
	  }
	  status++;
  }
  fprintf(fp2, "};\n\n#endif  // CHARACTER_H");
  fclose(fp2);
  fclose(fp);
  return 0;
}
